//
//  CommentManyReplyCell.m
//  IfengNews
//
//  Created by tianhy on 12-12-26.
//
//

#import "CommentManyReplyCell.h"
#import "CalculatHeightForReplayCell.h"
#import <QuartzCore/QuartzCore.h>
#import "Define.h"
#import "CTCommentTable.h"
#import "IfengCommentSingleFloorLabel.h"
#import "cutTouchEventView.h"
#import "PlanningTopicCoreTextLabel.h"
#import "DetailUtil.h"
#import "BlockUI.h"
#import "UIImageView+WebCache.h"
#import "UIView+Utils.h"
#import "SgrGCD.h"

@interface CommentManyReplyCell(Private)

- (UIView *)singleFloorViewWith:(NSDictionary *)dic;
- (IfengCommentSingleFloorLabel *)singleFloorViewWith:(NSDictionary *)dic
                                            withIndex:(NSNumber *)index
                                  needAddSepertorView:(BOOL)need;
- (UIView *)omissionView;
@end

@implementation CommentManyReplyCell
@synthesize dataDic,indexPath,delegate,isSupported,isSupportClick;
@synthesize isShowAllComments;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.isSupportClick = YES;
    }
    return self;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor{
    [super setBackgroundColor:backgroundColor];
    self.contentView.backgroundColor=backgroundColor;
}


#pragma mark -
#pragma mark Main views

- (void)setUpSubViews
{
    self.contentView.backgroundColor = RGB(0xf7, 0xf7, 0xf7);
    self.backgroundColor = RGB(0xf7, 0xf7, 0xf7);
    
    [self performSelector:@selector(clearSubViews)];
    [self performSelector:@selector(addbackgroundView)];
    [self performSelector:@selector(initIconView)];
    [self performSelector:@selector(initZoneView)];
    [self performSelector:@selector(initSupportView)];
    [self performSelector:@selector(initFloorsView)];
    [self performSelector:@selector(initCurrentCommentView)];
    [self performSelector:@selector(initSepertorView)];
 
//    if (!self.isSupportClick) {
//        [self performSelector:@selector(addSufaceView)];
//    }
}

#pragma mark -
#pragma mark Construct Cell views

- (void)clearSubViews
{
    [[self.contentView subviews] enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [(UIView *)obj removeFromSuperview];
    }];
}


- (void)addbackgroundView
{
    IfengLabel *backgroundView = [[IfengLabel alloc] init];
    backgroundView.userInteractionEnabled = YES;
    backgroundView.frame = CGRectMake(0, 0, 320, [CalculatHeightForReplayCell calculationHeightWithData:self.dataDic isShowAllComments:isShowAllComments]);
    backgroundView.tag = 10;
    backgroundView.level = 2;
    backgroundView.delegate = self;
    backgroundView.dataDic = self.dataDic;
    backgroundView.index = 0;
    backgroundView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:backgroundView];
}

- (void)initIconView
{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    UIImageView *iconImageView = [[UIImageView alloc] init];
    iconImageView.backgroundColor = [UIColor whiteColor];
    iconImageView.frame = CGRectMake(leftmarginOfIcon, 0, iconSize.width,iconSize.height);
    iconImageView.userInteractionEnabled = YES;
    iconImageView.layer.masksToBounds = YES;
    iconImageView.layer.cornerRadius = cornerRadiusOfIcon;
    [backgroundView addSubview:iconImageView];
    
    NSString *iconUrl = [self.dataDic sgrGetStringForKey:@"userFace"];
    if(![iconUrl isEqualToString:defaultUserIcon])
    {
        [iconImageView sd_setImageWithURL:[NSURL URLWithString:iconUrl]
                         placeholderImage:[UIImage imageNamed:@"comment_user_icon.png"]];
//        [iconImageView reset2DefaultWithUrl:iconUrl];
//        [iconImageView setUrl4Image:iconUrl];
    }
    else
    {
        iconImageView.image = [UIImage imageNamed:@"comment_user_icon.png"];
    }
    
//    NSString *ext2 = [dataDic sgrGetStringForKey:@"ext2"];
//    NSString *guid = nil;
//    if(_isStrNotNull(ext2)){
//        NSDictionary *dic = [ext2 JSONValue];
//        guid = [dic sgrGetStringForKey:@"guid"];
//    }
//    if(_isStrNULL(guid))
//    {
//        UIView *coverView = [UIView new];
//        coverView.frame = iconImageView.bounds;
//        coverView.userInteractionEnabled = YES;
//        coverView.layer.masksToBounds = YES;
//        coverView.layer.cornerRadius = 12.0;
//        coverView.alpha = 0.45;
//        coverView.backgroundColor = ICurrentTheme.commentStyle.headCoverViewColor;
//        [iconImageView addSubview:coverView];
//    }
    
    UIButton *userHeadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    userHeadBtn.frame = CGRectMake(iconImageView.frame.origin.x-10, iconImageView.frame.origin.y, iconImageView.frame.size.width+20, iconImageView.frame.size.height+10);
    userHeadBtn.backgroundColor = [UIColor clearColor];
    [userHeadBtn addTarget:self action:@selector(nameTapResponse:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:userHeadBtn];
    NSArray *arr1=[[self.dataDic sgrGetStringForKey:@"profile"] componentsSeparatedByString:@"&"];
   
    int icIndex=-1;
    for(NSString *str in arr1){
        if([str hasPrefix:@"groupId="]){
            icIndex=[[str stringByReplacingOccurrencesOfString:@"groupId=" withString:@""] intValue]-2;
            break;
        }
    }
    if(icIndex >=0){
        CGRect frame=[iconImageView convertRect:CGRectMake(iconImageView.width-9, iconImageView.height-9, 12, 12)
                                         toView:backgroundView];
        UIImageView *chan=[[UIImageView alloc] initWithFrame:frame];
        chan.image=[UIImage imageNamed:[[CLGloble sharedInstance].levelIcons sgrGetStringForIndex:icIndex]];
        [backgroundView addSubview:chan];
    }

}


- (void)initZoneView   //表示用户地域的Label
{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    

//    NSString *nickName = [Globle shareInstance].userEntity.userNickName;
//    NSString *name = [Globle shareInstance].userEntity.userName;
    NSString *userName = nil;
    //_isStrNotNull(nickName)?nickName:(_isStrNotNull(name)?name:@"匿名");
    NSString *ipFromStr = [dataDic sgrGetStringForKey:@"ip_from"];
   // NSString *locationStr = (ipFromStr&&([ipFromStr length] > 6))?[ipFromStr substringToIndex:6]:ipFromStr;
    
    NSString *zoneStr = nil;
    userName = [dataDic sgrGetStringForKey:@"uname"];
    if(_isStrNotNull(ipFromStr))
        userName=[NSString stringWithFormat:@"%@ • ",userName];

    
//    NSString *ext2 = [dataDic sgrGetStringForKey:@"ext2"];
//    NSString *guid = nil;
//    if(_isStrNotNull(ext2)){
//        NSDictionary *dic = [ext2 JSONValue];
//        guid = [dic sgrGetStringForKey:@"guid"];
//        userName = [dic sgrGetStringForKey:@"nickname"];
//    }
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:13];
    nameLabel.userInteractionEnabled = YES;
    nameLabel.backgroundColor = [UIColor clearColor];
    
    nameLabel.textColor = RGB(0x5c, 0x85, 0x9f);
    
//    if(userName.length > 8)
//    {
//        userName = [userName substringToIndex:8];
//    }
    nameLabel.text = userName;
    CGSize nameSize = [userName sizeWithFont:[UIFont systemFontOfSize:13]
                           constrainedToSize:CGSizeMake(1000, zoneHeight)
                               lineBreakMode:NSLineBreakByWordWrapping];
    nameLabel.frame = CGRectMake(leftMarginOfComment, intervalFromTop, nameSize.width, zoneHeight);
    [backgroundView addSubview:nameLabel];
    
//    UIButton *nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    nameBtn.frame = CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y-10, nameLabel.frame.size.width, nameLabel.frame.size.height+20);
//    nameBtn.backgroundColor = [UIColor clearColor];
//    [nameBtn addTarget:self action:@selector(nameTapResponse:) forControlEvents:UIControlEventTouchUpInside];
//    [backgroundView addSubview:nameBtn];
//    
    UILabel *zoneLabel = [[UILabel alloc] init];
    zoneLabel.tag = 1001;
    zoneLabel.numberOfLines = 1;
    zoneLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    zoneLabel.frame = CGRectMake(nameLabel.frame.origin.x + nameLabel.frame.size.width,intervalFromTop,100,zoneHeight);
    zoneLabel.text = _isStrNULL(ipFromStr)?@"":ipFromStr;
    zoneLabel.font = [UIFont systemFontOfSize:13];
    zoneLabel.textColor = RGB(0x86, 0x86, 0x86);
    zoneLabel.backgroundColor = [UIColor clearColor];
    
    [backgroundView addSubview:zoneLabel];
}

- (void)initSupportView
{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    
    UIView *supportView = [[UIView alloc] init];
    supportView.tag = 10001;
    //supportView.backgroundColor = ICurrentTheme.commentStyle.comment_bgColor;
    supportView.backgroundColor = [UIColor clearColor];
    supportView.frame = CGRectMake(backgroundView.width-rightMargin-70, intervalFromTop, 70, 12);
    [backgroundView addSubview:supportView];
    
    UIImageView *supportImgview = [[UIImageView alloc] init];
    supportImgview.tag = 10002;
    
    if(!isSupported)
        supportImgview.image = [UIImage imageNamed:@"comment_replay_support.png"];
    else
        supportImgview.image = [UIImage imageNamed:@"comment_replay_supported.png"];
    
    [supportView addSubview:supportImgview];
    
    UILabel *supportCountNumLabel = [[UILabel alloc] init];
    supportCountNumLabel.tag = 10003;
    // supportCountNumLabel.backgroundColor = ICurrentTheme.commentStyle.comment_bgColor;
    supportCountNumLabel.backgroundColor = [UIColor clearColor];
    supportCountNumLabel.textColor = RGB(0x86, 0x86, 0x86);
    supportCountNumLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:12.];
    supportCountNumLabel.numberOfLines = 0;
    
    NSString *supportStr = [self.dataDic sgrFGetStringForKey:@"uptimes"];
//    if(isSupported)
//    {
//        NSInteger supportCount = [supportStr integerValue];
//        supportCount++;
//        supportStr = [NSString stringWithFormat:@"%d",supportCount];
//    }
    
    
    if(!supportStr || [supportStr length] == 0)
        supportStr = @"0";
    
    if([supportStr isEqualToString:@"0"])
        supportStr = @"";
    
    supportCountNumLabel.text = supportStr;
    CGSize supportStrSize = [supportStr sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(50, 12)];
    supportCountNumLabel.frame=CGRectMake(supportView.width-supportStrSize.width, 0, supportStrSize.width, 12);
    supportImgview.frame = CGRectMake(CGRectGetWidth(supportView.frame)-supportImgview.width-10, 0, 14, 14);
    supportCountNumLabel.frame = CGRectMake(CGRectGetWidth(supportView.frame)-supportCountNumLabel.width-supportImgview.frame.size.width, 2, supportCountNumLabel.width, 12);
    [supportView addSubview:supportCountNumLabel];
    
    UIButton *supportButton=[UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame=supportView.frame;
    frame.size.height+=20;
    frame.origin.y-=10.f;
    frame.origin.x+=20.f;
    supportButton.frame=frame;
    [backgroundView addSubview:supportButton];
    __weak typeof(self) me=self;
    __weak UIView *sView=supportView;
    [supportButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
      //  if(!me.isSupported){
            if([self.delegate respondsToSelector:@selector(supportClickedWihtIndexPath:)]){
                
                
              
                    [[SgrGCD sharedInstance] enqueueForegroundWithDelay:0.5 block:^{
                        IfengLabel *backgroundView = (IfengLabel *)[me.contentView viewWithTag:10];
                        UIImageView *supportImageView = (UIImageView *)[backgroundView viewWithTag:10002];
                        
                        [me addOneAnimation:CGPointMake(supportImageView.right, supportImageView.top)
                                           :sView];
                        
                    }];
                
                
                
                NSMutableDictionary *dic=[NSMutableDictionary dictionary];
                [dic sgrSetObject:me.indexPath forKey:@"indexPath"];
                [dic sgrSetObject:me.dataDic forKey:@"sourceDic"];
                
                [me.delegate performSelector:@selector(supportClickedWihtIndexPath:)
                                    withObject:dic];
            }
       // }
    }];
    
}

- (void)addOneAnimation:(CGPoint )point :(UIView *)sView{
    UILabel *add=[UILabel new];
    add.font=[UIFont systemFontOfSize:8.f];
    add.textColor=RGB(0xf5, 0x43, 0x43);
    add.text=@"+1";
    CGRect frame=CGRectMake(point.x, point.y, 10.f, 10.f);
    
    add.frame=frame;
    [sView addSubview:add];
    
    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect frame1=add.frame;
                         frame1.origin.y-=11.f;
                         add.frame=frame1;
                         add.alpha=0.f;
                         add.font=[UIFont systemFontOfSize:10.f];
                     } completion:^(BOOL finished) {
                         [add removeFromSuperview];
                     }];
}

- (void)initTimeView
{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    UILabel *zoneLabel = (UILabel *)[backgroundView viewWithTag:101];
    
    NSString *commentTime = nil;
    if(dataDic && [dataDic isKindOfClass:[NSDictionary class]])
    {
        NSString *timeStr = [dataDic sgrFGetStringForKey:@"comment_date"];
        if(!timeStr || [timeStr length] == 0)
            return;
        commentTime = timeStr;
    }
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.backgroundColor = RGB(0xf7, 0xf7, 0xf7);
    timeLabel.tag  = 104;
    timeLabel.numberOfLines = 1;
    timeLabel.frame = CGRectMake(zoneLabel.frame.origin.x + zoneLabel.frame.size.width + 20, zoneLabel.frame.origin.y, 200, zoneLabel.frame.size.height);
    timeLabel.text = commentTime;
    timeLabel.font = [UIFont systemFontOfSize:14];
    timeLabel.textColor =RGB(0xa6, 0xa6, 0xa6);
    [backgroundView addSubview:timeLabel];
}

- (void)initFloorsView     //初始化盖楼的视图
{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    UILabel *zoneLabel = (UILabel *)[backgroundView viewWithTag:1001];
   // IfengUILabel *floorsBottomView = [[IfengUILabel alloc] init];
    UIView *floorsBottomView=[[UIView alloc] init];
    floorsBottomView.backgroundColor = RGB(0xed, 0xf0, 0xf2);
    floorsBottomView.userInteractionEnabled = YES;
    floorsBottomView.tag =102;
    float origin_y = zoneLabel.frame.origin.y + zoneLabel.frame.size.height + intervalZoneAndComment;
    float height = [CalculatHeightForReplayCell calculationFloorsHeight:self.dataDic
                                                      isShowAllComments:isShowAllComments];
    floorsBottomView.frame = CGRectMake(leftMarginOfComment, origin_y, 254, height);
    
    if(!dataDic || ![dataDic isKindOfClass:[NSDictionary class]]){
        return;
    }
    
    NSArray *parentCommentArray = [dataDic sgrGetArrayForKey:@"parent"];
    if(!parentCommentArray
       || ![parentCommentArray isKindOfClass:[NSArray class]]
       || [parentCommentArray count] <= 0){
        return;
    }
    
    
    cutTouchEventView *bgView = [[cutTouchEventView alloc] initWithFrame:CGRectMake(0, floorsBottomView.frame.origin.y, backgroundView.frame.size.width, floorsBottomView.frame.size.height)];
    bgView.userInteractionEnabled = YES;
    bgView.backgroundColor = [UIColor clearColor];
    bgView.alpha = 0.5;
    [backgroundView addSubview:bgView];
 
    
    [backgroundView addSubview:floorsBottomView];
    [floorsBottomView layer].borderWidth = 0.5;
    [floorsBottomView layer].borderColor = RGB(0xd8, 0xd7, 0xd7).CGColor;

    
    //显示成包含隐藏楼层的样式
    if([parentCommentArray count]>6 && !isShowAllComments)
    {
        [self constructFoldView:dataDic withSuperView:floorsBottomView];

    }
    else    //显示成很多评论楼层平铺组合在一起的样式
    {
        [self constructFloorView:dataDic withSuperView:floorsBottomView];

    }
    
    
    
    
    

   // backgroundView.backgroundColor=[UIColor orangeColor];
    
    /*
     [[floorsBottomView layer] setShadowOffset:CGSizeMake(0,0)];
     [[floorsBottomView layer] setShadowRadius:2];
     [[floorsBottomView layer] setShadowOpacity:0.3];
     [[floorsBottomView layer] setShadowColor:[UIColor blackColor].CGColor];
     [floorsBottomView layer].masksToBounds = NO;
     floorsBottomView.layer.shadowPath = [UIBezierPath bezierPathWithRect:floorsBottomView.layer.bounds].CGPath;
     */
}

- (void)initCurrentCommentView
{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    //  UILabel *backgroundView = (UILabel *)[self viewWithTag:10];
    PlanningTopicCoreTextLabel *currentCommentLabel = [[PlanningTopicCoreTextLabel alloc] init];
    //currentCommentLabel.backgroundColor = ICurrentTheme.commentStyle.comment_bgColor;
    currentCommentLabel.backgroundColor = [UIColor clearColor];
    //  currentCommentLabel.level = 1;
    //  currentCommentLabel.delegate = self;
    //  currentCommentLabel.dataDic = self.dataDic;
    currentCommentLabel.tag = 103;
    currentCommentLabel.lineSpace = commentLineSpace;
    currentCommentLabel.font = commentFont;
    currentCommentLabel.textColor = RGB(0x22, 0x22, 0x22);
    currentCommentLabel.backgroundColor = [UIColor clearColor];
    currentCommentLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
    
    UILabel *floorsView = (UILabel *)[backgroundView viewWithTag:102];
    float orinin_y = floorsView.frame.origin.y + floorsView.frame.size.height + 17;
    NSString *commentStr = [self.dataDic sgrFGetStringForKey:@"comment_contents"];
    commentStr = [commentStr stringByReplacingOccurrencesOfString:@"<br>" withString:@""];
    commentStr = [commentStr stringByReplacingOccurrencesOfString:@"&quot" withString:@""];
    //    float height = [[Globle shareInstance] getSizeOfText:commentStr
    //                                              withFont:commentFont
    //                                           withMaxSize:commentMaxSize].height;
    
    //    float height = [DetailUtil lineByWidth:commentStr Width:commentMaxSize.width
    //                                      Font:[UIFont systemFontOfSize:16.0f] lineHeight:26].height;
    float height = [[CLGloble sharedInstance] calculateSizeWithFont:commentFont
                                        withText:commentStr
                                     withMaxSize:commentMaxSize
                                   withLineSpace:commentLineSpace
                                 withAllowIndent:NO].height;
    currentCommentLabel.frame = CGRectMake(leftMarginOfComment, orinin_y, commentMaxSize.width, height);
    currentCommentLabel.text = commentStr;
    //    currentCommentLabel.textColor = ICurrentTheme.commentStyle.commentLabelTextColor;
    [backgroundView addSubview:currentCommentLabel];
}

- (void)initSepertorView
{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    UIImageView *sepertorView = [[UIImageView alloc] init];
    sepertorView.frame = CGRectMake(53,backgroundView.frame.size.height-0.5, 254, 0.5);
    sepertorView.image = [UIImage imageNamed:@"seperator_line.png"];
    sepertorView.backgroundColor = [UIColor clearColor];
    [backgroundView addSubview:sepertorView];
    [backgroundView bringSubviewToFront:sepertorView];
    
}
-(void)addSufaceView{
    IfengLabel *backgroundView = (IfengLabel *)[self.contentView viewWithTag:10];
    UIView *sufaceView = [[UIView alloc] initWithFrame:backgroundView.frame];
    sufaceView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:sufaceView];
    [self.contentView bringSubviewToFront:sufaceView];
}

#pragma mark -
#pragma mark Construct floor views

- (void)constructFoldView:(NSDictionary *)data  withSuperView:(UIView *)superView
//构建评论折叠在一起的，包涵隐藏楼层的视图
{
    @autoreleasepool {
        NSArray *parentCommentArray = [data sgrGetArrayForKey:@"parent"];
        float origin_y = 0;     //每一个视图纵坐标的开始的位置
        
        NSDictionary *firstComment = [parentCommentArray sgrGetDictionaryForIndex:[parentCommentArray count]-1];
        
        UIView *firstCommentView = [self singleFloorViewWith:firstComment
                                                   withIndex:[NSNumber numberWithInt:1]
                                         needAddSepertorView:NO];
        firstCommentView.frame = CGRectMake(0, origin_y, firstCommentView.frame.size.width,
                                            firstCommentView.frame.size.height);
        [superView addSubview:firstCommentView];
        origin_y = origin_y + firstCommentView.frame.size.height;
        
        //添加点击显示更多的视图
        NSArray *allFloors = [data sgrGetArrayForKey:@"parent"];
        int floorNum = 0;
        if(allFloors && [allFloors isKindOfClass:[NSArray class]])
            floorNum = [allFloors count];
        else
            floorNum = 0;
        
        NSString *buttonTitle = [NSString stringWithFormat:@"展开全部%d条",floorNum-4];
        UIButton *showMoreButton = [self showMoreViewWithTitle:buttonTitle];
        [showMoreButton addTarget:self action:@selector(showHiddenedComments)
                 forControlEvents:UIControlEventTouchUpInside];
        
        showMoreButton.frame = CGRectMake(0, origin_y, showMoreButton.frame.size.width,
                                          showMoreButton.frame.size.height);
        origin_y = origin_y + showMoreButton.frame.size.height;
        
        [superView addSubview:showMoreButton];
        
        for (int i = 2; i >= 0; i--)
        {
            UIView *floorView = [self singleFloorViewWith:[parentCommentArray objectAtIndex:i]
                                                withIndex:[NSNumber numberWithInt:floorNum - i]
                                      needAddSepertorView:(i==0)?NO:YES];
            floorView.frame = CGRectMake(0, origin_y, floorView.size.width, floorView.size.height);
            origin_y = origin_y + floorView.size.height;
            [superView addSubview:floorView];
        }
    }
}

- (void)contructHalfFlodView:(NSDictionary *)data withSuperView:(UIView *)superView
{
    @autoreleasepool {
        NSArray *parentCommentArray = [data sgrGetArrayForKey:@"parent"];
        float origin_y = 0;     //每一个视图纵坐标的开始的位置
        int totalShowFloors = 3;
        
        int commentsCount = [parentCommentArray count];
        for(int j=0; j<totalShowFloors; j++){ //构建前3楼的评论
            BOOL needsepertorView = (j==1)?NO:YES;
            NSDictionary *comment = [parentCommentArray sgrGetDictionaryForIndex:commentsCount-1];
            UIView *floorView = [self singleFloorViewWith:comment
                                                withIndex:[NSNumber numberWithInt:j+1]
                                      needAddSepertorView:needsepertorView];
            floorView.frame = CGRectMake(0, origin_y, floorView.frame.size.width, floorView.frame.size.height);
            [superView addSubview:floorView];
            origin_y = origin_y + floorView.frame.size.height;
        }
        
        //中间的省略号的视图
        UIView *omissionView = [self omissionView];
        omissionView.frame = CGRectMake(0, origin_y, omissionView.frame.size.width, omissionView.frame.size.height);
        [superView addSubview:omissionView];
        origin_y = origin_y + omissionView.frame.size.height;
        
        for(int i=totalShowFloors; i>0; i--){  //构建最后3楼的评论
            BOOL needsepertorView = (i==totalShowFloors-1)?NO:YES;
            NSDictionary *comment = [parentCommentArray objectAtIndex:i];
            UIView *floorView = [self singleFloorViewWith:comment
                                                withIndex:[NSNumber numberWithInt:commentsCount-i+1]
                                      needAddSepertorView:needsepertorView];
            floorView.frame = CGRectMake(0, origin_y, floorView.frame.size.width, floorView.frame.size.height);
            [superView addSubview:floorView];
            origin_y = origin_y + floorView.frame.size.height;
        }
        
    }
}



//构建评论平铺的视图
- (void)constructFloorView:(NSDictionary *)data  withSuperView:(UIView *)aSuperView
{
    //NSLog(@"%@",[NSThread isMainThread]?@"yes":@"no");
        NSArray *parentCommentArray = [data sgrGetArrayForKey:@"parent"];
        if(!parentCommentArray || ![parentCommentArray isKindOfClass:[NSArray class]]
           || [parentCommentArray count] <= 0)
            return;
        float origin_y = 0;     //每一个视图纵坐标的开始的位置
        int parentCommentCount = [parentCommentArray count];
    
   
   
    
        for(int i=0; i<parentCommentCount; i++)
        {
            BOOL isNeedSepertorView = (i == parentCommentCount-1)?NO:YES;
            NSDictionary *dic = [parentCommentArray sgrGetDictionaryForIndex:parentCommentCount-1-i];
            
            IfengCommentSingleFloorLabel *oneFloorView = [self singleFloorViewWith:dic
                                                                         withIndex:[NSNumber numberWithInt:i+1]
                                                               needAddSepertorView:isNeedSepertorView];
            oneFloorView.frame = CGRectMake(0,origin_y, oneFloorView.frame.size.width,oneFloorView.frame.size.height);
            [aSuperView addSubview:oneFloorView];
            
            
        
            origin_y = origin_y + oneFloorView.frame.size.height;
           
        }
//    aSuperView.backgroundColor=[UIColor orangeColor];
    
}

- (IfengCommentSingleFloorLabel *)singleFloorViewWith:(NSDictionary *)dic
                      withIndex:(NSNumber *)index
            needAddSepertorView:(BOOL)need
{
    //此处的index是从1开始计算的，需要注意
    IfengCommentSingleFloorLabel *backgroundView = [[IfengCommentSingleFloorLabel alloc] init];
    backgroundView.backgroundColor = RGB(0xed, 0xf0, 0xf2);
    //  backgroundView.delegate = self;
    backgroundView.tag = 1000;
    //  backgroundView.level = 2;
    //  backgroundView.dataDic = dic;
    //  backgroundView.index = [index integerValue];
    backgroundView.index = [index integerValue];
    backgroundView.superCell = self;
    
//    NSString *nickName = [Globle shareInstance].userEntity.userNickName;
//    NSString *name = [Globle shareInstance].userEntity.userName;
    NSString *userName = nil;
    NSString *ipFromStr = [dic sgrGetStringForKey:@"ip_from"];
//    if(ipFromStr && ipFromStr.length > 6)
//        ipFromStr = [ipFromStr substringToIndex:6];
//    NSString *zoneStr = [NSString stringWithFormat:@" • %@",_isStrNotNull(ipFromStr)?ipFromStr:@"未知地区"];
    NSString *zoneStr = nil;
    if(_isStrNotNull(ipFromStr))
        zoneStr = ipFromStr;
    else
        zoneStr = @"";
    
//    NSString *ext2 = [dic sgrGetStringForKey:@"ext2"];
//    NSString *guid = nil;
//    if(_isStrNotNull(ext2)){
//        NSDictionary *dic = [ext2 JSONValue];
//        guid = [dic sgrGetStringForKey:@"guid"];
//        userName = [dic sgrGetStringForKey:@"nickname"];
//    }
    userName =[dic sgrGetStringForKey:@"uname"];
    if(_isStrNotNull(zoneStr)){
        userName = [NSString stringWithFormat:@"%@ • ",userName];
    }
    
    NSString *commentStr = [dic sgrGetStringForKey:@"comment_contents"];
    commentStr = [commentStr stringByReplacingOccurrencesOfString:@"<br>" withString:@""];
    commentStr = [commentStr stringByReplacingOccurrencesOfString:@"&quot" withString:@""];
    float height = [CalculatHeightForReplayCell calculationOneFloorHeight:commentStr];
    backgroundView.frame = CGRectMake(0, 0, 254, height);
    
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:13];
    nameLabel.userInteractionEnabled = YES;
    nameLabel.backgroundColor = [UIColor clearColor];
//    if(_isStrNotNull(guid)){ //表示是登陆用户，可以点击
//        nameLabel.textColor = ICurrentTheme.commentStyle.commentNameTextColor;
//    }
//    else{
        nameLabel.textColor = RGB(0x5c, 0x85, 0x9f);
    //}
//    if(userName.length > 8)
//        userName = [userName substringToIndex:8];
    nameLabel.text = userName;
    CGSize nameSize = [userName sizeWithFont:[UIFont systemFontOfSize:13]
                           constrainedToSize:CGSizeMake(1000, zoneHeight)
                               lineBreakMode:NSLineBreakByWordWrapping];
    nameLabel.frame = CGRectMake(11, 12, nameSize.width, zoneHeight);
    [backgroundView addSubview:nameLabel];
    
    UIButton *nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nameBtn.frame = nameLabel.bounds;
    nameBtn.backgroundColor = [UIColor clearColor];
    nameBtn.tag = 100+[index intValue];
    [nameBtn addTarget:self action:@selector(nameTapResponse:) forControlEvents:UIControlEventTouchUpInside];
    [nameLabel addSubview:nameBtn];
    
    UILabel *zoneLabel = [[UILabel alloc] init];
    zoneLabel.numberOfLines = 1;
    zoneLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    zoneLabel.frame = CGRectMake(nameLabel.right,nameLabel.top,GlobleHeight-80.f,zoneHeight);
    zoneLabel.text = zoneStr;
    zoneLabel.font = [UIFont systemFontOfSize:13];
    zoneLabel.textColor = RGB(0x86, 0x86, 0x86);
    zoneLabel.backgroundColor = [UIColor clearColor];
    [backgroundView addSubview:zoneLabel];
    
    //添加楼层的Label
    UILabel *indexLabel = [[UILabel alloc] init];
    indexLabel.userInteractionEnabled = YES;
    indexLabel.backgroundColor = [UIColor clearColor];
    indexLabel.text = [NSString stringWithFormat:@"%@",index];
    indexLabel.font = [UIFont systemFontOfSize:12];
    indexLabel.textColor = RGB(0xa6, 0xa6, 0xa6);
    indexLabel.frame = CGRectMake(215,10,30,zoneHeight);
    indexLabel.textAlignment = NSTextAlignmentRight;
    [backgroundView addSubview:indexLabel];
    
    float commentHeight = [[CLGloble sharedInstance] calculateSizeWithFont:commentFont
                                               withText:commentStr
                                            withMaxSize:commentMaxSizeInFloors
                                          withLineSpace:commentLineSpace
                                        withAllowIndent:NO].height;
    
    PlanningTopicCoreTextLabel *commentLabel = [[PlanningTopicCoreTextLabel alloc] init];
    commentLabel.userInteractionEnabled = NO;
    commentLabel.font = commentFont;
    commentLabel.lineSpace = commentLineSpace;
    commentLabel.frame = CGRectMake(10,42,commentMaxSizeInFloors.width,commentHeight);//38
    commentLabel.textColor = RGB(0x22, 0x22, 0x22);
    commentLabel.text = commentStr;
    commentLabel.backgroundColor = [UIColor clearColor];
    commentLabel.autoresizingMask=UIViewAutoresizingFlexibleHeight;
    [backgroundView addSubview:commentLabel];
    
    if(need)
    {
        UIImageView *bottomSeperatorView = [[UIImageView alloc] init];
        bottomSeperatorView.frame = CGRectMake(0,height - 0.5,backgroundView.width,0.5);
        bottomSeperatorView.backgroundColor = RGB(0xd8, 0xd7, 0xd7);
        [backgroundView addSubview:bottomSeperatorView];
    }
    
    return backgroundView;
}

- (UIButton *)showMoreViewWithTitle:(NSString *)title
{
    UIButton *showMoreButton = [[UIButton alloc] init];
    showMoreButton.backgroundColor = RGB(0xed, 0xf0, 0xf2);
    showMoreButton.frame = CGRectMake(0,0,254, 50);
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.frame = showMoreButton.bounds;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.backgroundColor =RGB(0xed, 0xf0, 0xf2);
    titleLabel.textColor = RGB(0x94, 0x94, 0x94);
    titleLabel.text = title;
    
    titleLabel.layer.borderColor = RGB(0xd8, 0xd7, 0xd7).CGColor;
    titleLabel.layer.borderWidth = 0.5;
    
    [showMoreButton addSubview:titleLabel];
    
    return showMoreButton;
}

- (UIView *)omissionView
{
    UIView *omissinView = [[UIView alloc] init];
    omissinView.backgroundColor = [UIColor whiteColor];
    omissinView.frame = CGRectMake(0, 0, 234, 40);
    
    UIImageView *omissImageView = [[UIImageView alloc] init];
    omissImageView.frame = CGRectMake(0, 0, 3,19);
    omissImageView.center = omissinView.center;
    omissImageView.image = [UIImage imageNamed:@"comment_omission.png"];
    [omissinView addSubview:omissImageView];
    
    UIImageView *bottomSeperatorView = [[UIImageView alloc] init];
    bottomSeperatorView.frame = CGRectMake(0,omissinView.frame.size.height-0.5,omissinView.frame.size.width,0.5);
    bottomSeperatorView.image = [UIImage imageNamed:@"seperator_line.png"];
    [omissinView addSubview:bottomSeperatorView];
    
    UIImageView *topSeperatorView = [[UIImageView alloc] init];
    topSeperatorView.frame = CGRectMake(0,0,omissinView.frame.size.width, 0.5);
    topSeperatorView.image = [UIImage imageNamed:@"seperator_line.png"];
    [omissinView addSubview:topSeperatorView];
    
    return omissinView;
}

#pragma mark -
#pragma mark IfengLabel Delegate Methods
- (void)ifengLabelDidSingleClickAtIndex:(NSNumber *)index
                               withData:(NSDictionary *)dic
                              withLevel:(NSNumber *)level
                     withLocateInWindow:(CGPoint)locateInWindow
                       withLocateInCell:(CGPoint)locateInCell
                         withSizeOfCell:(CGSize)sizeOfCell
                               withView:(UIView *)view
{
    if(!self.isSupportClick)
        return;
    int iLevel = [level intValue];
    if(iLevel == 1)   //最底层的视图点击事件，表示回复当前评论
    {
        //do something
    }else if(iLevel == 2) //单独的楼层的点击事件，表示回复某一层楼的评论
    {
        //do something
    }
    if(self.delegate && [self.delegate respondsToSelector:@selector(addReplayAtIndexPath:withLocateInWindow:withLocateInCell:withSizeOfCell:withSourceDic:atParentsArrayIndex:withLevel:withView:)]){
        [(CTCommentTable *)self.delegate addReplayAtIndexPath:indexPath
                                           withLocateInWindow:locateInWindow
                                             withLocateInCell:locateInCell
                                               withSizeOfCell:sizeOfCell
                                                withSourceDic:dic
                                          atParentsArrayIndex:[index integerValue]
                                                    withLevel:level
                                                     withView:view];
    }
}

#pragma mark - 
#pragma mark  Show More Comments Methods
- (void)showHiddenedComments
{
    if(delegate && [delegate respondsToSelector:@selector(showHiddedComments:)])
    {
        [delegate performSelector:@selector(showHiddedComments:) withObject:self.indexPath];
    }
}



#pragma mark -
#pragma mark Support
- (void)supportComment
{
    IfengLabel *backgroundView = (IfengLabel *)[self viewWithTag:10];
    UIView *supportView = (UIView *)[backgroundView viewWithTag:10001];
    UIImageView *supportImageView = (UIImageView *)[supportView viewWithTag:10002];
    UILabel *supportCountLabel = (UILabel *)[supportView viewWithTag:10003];
    
    [supportImageView setImage:[UIImage imageNamed:@"comment_replay_supported.png"]];
    NSString *supportedNumStr = [NSString stringWithFormat:@"%d",[supportCountLabel.text intValue]+1];
    supportCountLabel.text = supportedNumStr;
    CGSize supportStrSize = [supportedNumStr sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(50, 12)];
//    supportImageView.frame = CGRectMake(supportView.width-supportImageView.width, 0, supportImageView.width, supportImageView.height);
    supportCountLabel.frame = CGRectMake(supportView.width-supportImageView.width-supportStrSize.width, 2, supportStrSize.width, 12);
}

- (void)nameTapResponse:(id)sender
{
    if(delegate && [delegate respondsToSelector:@selector(nameTapWithData:withIndexPath:)])
    {
        NSDictionary *paragram = nil;
        UIButton *btn = (UIButton *)sender;
        NSArray *parents = [self.dataDic sgrGetArrayForKey:@"parent"];
        int parentsCount = [parents count];
        int index = btn.tag - 100 - 1;      //此处减去一是因为，索引是从1开始的
        
        if(btn.tag >= 100)  //表示是楼层的用户名点击
        {
            
            paragram = [parents sgrGetDictionaryForIndex:parentsCount-1-index];
        }
        else  //表示是当前评论的用户名点击
        {
            paragram = self.dataDic;
        }
        [delegate performSelector:@selector(nameTapWithData:withIndexPath:)
                       withObject:paragram
                       withObject:self.indexPath];
    }
}
@end
